package com.example.allplan.ItemTouchHelper;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

public class JiHuaBaoTouchCallback extends ItemTouchHelper.Callback {
    public JiHuaBaoTouchCallback(itemtouchhelpercallback mc) {
        mcallback=mc;
    }
    itemtouchhelpercallback mcallback;
    public interface itemtouchhelpercallback{
        void onitemmove(int frompos,int topos);
        void onitemswipe(int direction);
    }
    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //表示侧滑效果支持的方向
        int swipeFlags = ItemTouchHelper.LEFT ;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        mcallback.onitemmove(viewHolder.getAdapterPosition(), target.getAdapterPosition());

        return true;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        mcallback.onitemswipe(viewHolder.getAdapterPosition());
    }
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        return 0.5f;
    }
    //返回值滑动消失的距离，滑动小于这个值不消失，大于消失

    @Override
    public float getSwipeEscapeVelocity(float defaultValue) {
        return 2f;
    }

    //设置手指离开后ViewHolder的动画时间
    @Override
    public long getAnimationDuration(RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
        return 100;
    }

    //网格型RecyclerView
    @Override
    public float getMoveThreshold(RecyclerView.ViewHolder viewHolder) {
        return 0.9f;
    }

    //返回值决定是否有滑动操作
    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

}
